/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package eistar.impl;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import ms.unicorn.dsl.eistar.util.EistarUtil;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import eistar.EistarPackage;
import eistar.Entity;
import eistar.EvolutionAddOperation;
import eistar.EvolutionBranch;
import eistar.EvolutionOperation;
import eistar.EvolutionRemoveOperation;
import eistar.EvolutionRule;
import eistar.Model;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Evolution Branch</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link eistar.impl.EvolutionBranchImpl#getOperations <em>Operations</em>}</li>
 *   <li>{@link eistar.impl.EvolutionBranchImpl#getProbability <em>Probability</em>}</li>
 *   <li>{@link eistar.impl.EvolutionBranchImpl#getImportedNamespace <em>Imported Namespace</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class EvolutionBranchImpl extends EntityImpl implements EvolutionBranch {
	/**
	 * The cached value of the '{@link #getOperations() <em>Operations</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getOperations()
	 * @generated
	 * @ordered
	 */
	protected EList<EvolutionOperation> operations;

	/**
	 * The default value of the '{@link #getProbability() <em>Probability</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProbability()
	 * @generated
	 * @ordered
	 */
	protected static final int PROBABILITY_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getProbability() <em>Probability</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProbability()
	 * @generated
	 * @ordered
	 */
	protected int probability = PROBABILITY_EDEFAULT;

	/**
	 * The default value of the '{@link #getImportedNamespace() <em>Imported Namespace</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getImportedNamespace()
	 * @generated
	 * @ordered
	 */
	protected static final String IMPORTED_NAMESPACE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getImportedNamespace() <em>Imported Namespace</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getImportedNamespace()
	 * @generated
	 * @ordered
	 */
	protected String importedNamespace = IMPORTED_NAMESPACE_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EvolutionBranchImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EistarPackage.Literals.EVOLUTION_BRANCH;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<EvolutionOperation> getOperations() {
		if (operations == null) {
			operations = new EObjectContainmentEList<EvolutionOperation>(EvolutionOperation.class, this, EistarPackage.EVOLUTION_BRANCH__OPERATIONS);
		}
		return operations;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getProbability() {
		return probability;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setProbability(int newProbability) {
		int oldProbability = probability;
		probability = newProbability;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EistarPackage.EVOLUTION_BRANCH__PROBABILITY, oldProbability, probability));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String getImportedNamespace() {
		return importedNamespace;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setImportedNamespace(String newImportedNamespace) {
		String oldImportedNamespace = importedNamespace;
		importedNamespace = newImportedNamespace;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EistarPackage.EVOLUTION_BRANCH__IMPORTED_NAMESPACE, oldImportedNamespace, importedNamespace));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings("unchecked")
	public <T extends Entity> List<T> getModelElement(Class<? extends T> eClass) {
		 Model original = getRule().getOriginal();
		 List<T> list = original.getModelElement(eClass);
		 Set<Entity> closure = new TreeSet<Entity>();
		 
		 
		 for(EvolutionRemoveOperation op: this.getMembers(EvolutionRemoveOperation.class)) {
			EistarUtil.findClosure((List<Entity>)list, op.getEntity(), closure, Entity.REFER_TO_ME, null);
			EistarUtil.findClosure((List<Entity>)list, op.getChildren(), closure, Entity.REFER_TO_ME, null);
		 }
		 
		 for (int i = list.size() - 1; i >= 0; i--)
			 if (closure.contains(list.get(i)))
					 list.remove(i);
		 
		 for(EvolutionAddOperation op: this.getMembers(EvolutionAddOperation.class)) 
			 for (Entity e: op.getChildren())
				 if (eClass.isInstance(e))
					 list.add((T) e);
		 return list;
	}

	@Override
	public EvolutionRule getRule() {
		return (EvolutionRule) eContainer;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isParentOf(Model model) {
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		throw new UnsupportedOperationException();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isEquivelent(Model model) {
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		throw new UnsupportedOperationException();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_BRANCH__OPERATIONS:
				return ((InternalEList<?>)getOperations()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_BRANCH__OPERATIONS:
				return getOperations();
			case EistarPackage.EVOLUTION_BRANCH__PROBABILITY:
				return getProbability();
			case EistarPackage.EVOLUTION_BRANCH__IMPORTED_NAMESPACE:
				return getImportedNamespace();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_BRANCH__OPERATIONS:
				getOperations().clear();
				getOperations().addAll((Collection<? extends EvolutionOperation>)newValue);
				return;
			case EistarPackage.EVOLUTION_BRANCH__PROBABILITY:
				setProbability((Integer)newValue);
				return;
			case EistarPackage.EVOLUTION_BRANCH__IMPORTED_NAMESPACE:
				setImportedNamespace((String)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_BRANCH__OPERATIONS:
				getOperations().clear();
				return;
			case EistarPackage.EVOLUTION_BRANCH__PROBABILITY:
				setProbability(PROBABILITY_EDEFAULT);
				return;
			case EistarPackage.EVOLUTION_BRANCH__IMPORTED_NAMESPACE:
				setImportedNamespace(IMPORTED_NAMESPACE_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_BRANCH__OPERATIONS:
				return operations != null && !operations.isEmpty();
			case EistarPackage.EVOLUTION_BRANCH__PROBABILITY:
				return probability != PROBABILITY_EDEFAULT;
			case EistarPackage.EVOLUTION_BRANCH__IMPORTED_NAMESPACE:
				return IMPORTED_NAMESPACE_EDEFAULT == null ? importedNamespace != null : !IMPORTED_NAMESPACE_EDEFAULT.equals(importedNamespace);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (probability: ");
		result.append(probability);
		result.append(", importedNamespace: ");
		result.append(importedNamespace);
		result.append(')');
		return result.toString();
	}

} //EvolutionBranchImpl
